package com.hello;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
public class TimePicker extends Activity {

	private int mHour;
	private int mMinute;
	private static TimePickerDialog timePickerDialog;
	static final int TIME_DIALOG_ID = 0;
	private static String time;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timepicker);
		String[] hm = time.split(":"); 
		mHour =  Integer.parseInt(hm[0]);
		mMinute = Integer.parseInt(hm[1]);
		showDialog(TIME_DIALOG_ID);		
	}

	// updates the time we display in the TextView
	private void updateTime() {
		time = pad(mHour)+":"+pad(mMinute);
	}
	
	public static String getTime() {
		return time;
	}
	
	public static void setTime(String t){
		time = t;
	}
	
	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}
	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
		new TimePickerDialog.OnTimeSetListener() {

		public void onTimeSet(android.widget.TimePicker view,
				int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute; 
			updateTime();
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
		}};

		@Override
		protected Dialog onCreateDialog(int id) {
			switch (id) {
			case TIME_DIALOG_ID: 
				timePickerDialog = new TimePickerDialog(this,
						mTimeSetListener, mHour, mMinute, true);
				 timePickerDialog.setOnDismissListener(new OnDismissListener(){
					 public void onDismiss(DialogInterface dialog) {
						 	Intent intent = new Intent();
							setResult(RESULT_CANCELED, intent);
				            finish();
				          }
				 });
				 return timePickerDialog;
			}
			return null;
		}

		
}